﻿using System;
using System.ComponentModel;
using System.IO;

namespace ComicReader.Model
{
    public interface IConversionService
    {
        void Convert(DirectoryInfo directoryInfo);
    }

    public class ConversionService : INotifyPropertyChanged, IConversionService
    {

        public void Convert(DirectoryInfo directoryInfo)
        {
            //RenameCbrToRar(directoryInfo);
            //CbrToCbz(directoryInfo);
        }

        public FileSystemInfo RootFileSystemInfo { get; set; }

        private void CbrToCbz(FileSystemInfo directoryInfo)
        {
            var files = Directory.GetFiles(directoryInfo.FullName);
            foreach (var sourceFile in files)
            {
                var extension = Path.GetExtension(sourceFile);
                switch (extension)
                {
                    case ".cbr":
                    case ".rar":
                        var cbzDestinationFile = sourceFile.Replace(extension, "");
                        var unrar = new Unrar
                                    {
                                        DestinationPath = cbzDestinationFile
                                    };

                        unrar.Open(sourceFile, Unrar.OpenMode.Extract);
                        // Extract each file found in hashtable
                        while (unrar.ReadHeader())
                        {
                            unrar.Extract();
                        }


                        break;
                    default:
                        break;
                }

            }

            var directories = Directory.GetDirectories(directoryInfo.FullName);
            foreach (var directory in directories)
            {
                CbrToCbz(new DirectoryInfo(directory));
            }
        }

        private void RenameCbrToRar(FileSystemInfo directoryInfo)
        {
            var files = Directory.GetFiles(directoryInfo.FullName);
            foreach (var sourceFile in files)
            {
                var extension = Path.GetExtension(sourceFile);
                switch (extension)
                {
                    case ".cbr":
                        var rarDestinationFile = sourceFile.Replace(extension, ".rar");
                        File.Move(sourceFile, rarDestinationFile);
                        break;
                    case ".zip":
                        var cbzDestinationFile = sourceFile.Replace(extension, ".cbz");
                        File.Move(sourceFile, cbzDestinationFile);
                        break;
                    default:
                        break;
                }

            }
            
            var directories = Directory.GetDirectories(directoryInfo.FullName);
            foreach (var directory in directories)
            {
                Convert(new DirectoryInfo(directory));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}
